package com.tg.appcommon.android;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.appbase.custom.app.AppLoginStatus;
import com.appbase.custom.app.AppLoginStatusChangeListener;
import com.tg.appcommon.app.AppLifecycleCallbacks;

import java.util.HashMap;

public class TGApplicationBase {
    public final static String APP_TOKEN = "TGApp_token";
    public final static String TRACK_NO = "device_track_no";
    public final static String ADD_DEVICE_FROM = "add_device_from";
    private final HashMap<Object, Object> mGlobalObjects = new HashMap<>();
    private Application mApplication;
    private Context mBase;
    private Context mConfigurationContext;
    private long mBackgroundTime;
    private AppLoginStatusChangeListener mAppLoginStatusChangeListener;

    public void setBackgroundTime() {
        mBackgroundTime = System.currentTimeMillis();
    }
    public long getBackgroundTime(){
        return mBackgroundTime;
    }
    public static boolean isBackGround() {
        return AppLifecycleCallbacks.getActivityNumber() == 0;
    }
    private TGApplicationBase() {
        mGlobalObjects.put(TRACK_NO, 0);
    }

    public static TGApplicationBase getInstance() {
        return LAZY_LOAD.sInstance;
    }

    private static final AppLifecycleCallbacks mAppLifecycleCallbacks = new AppLifecycleCallbacks();
    public  static AppLifecycleCallbacks getAppLifecycleCallbacks(){
        return mAppLifecycleCallbacks;
    }


    public Activity getNoFinishActivity() {
        return mAppLifecycleCallbacks.getNoFinishActivity();
    }

    public void finishActivity(String className) {
        mAppLifecycleCallbacks.finishActivity(className);
    }

    public void finishAllActivity() {
        mAppLifecycleCallbacks.finishAllActivity();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public Activity getActivity(String className){
        return mAppLifecycleCallbacks.getActivity(className);
    }

    @Nullable
    public Activity getCurrentActivity() {
        return mAppLifecycleCallbacks.getCurrentActivity();
    }

    public void setAppLoginStatusChangeListener(AppLoginStatusChangeListener appLoginStatusChangeListener) {
        mAppLoginStatusChangeListener = appLoginStatusChangeListener;
    }

    public void onLoginStatusChange(@AppLoginStatus int status){
        if (mAppLoginStatusChangeListener != null){
            mAppLoginStatusChangeListener.onLoginStatusChange(status);
        }
    }

    public void printActivity(){
        mAppLifecycleCallbacks.printActivity();
    }

    /**
     * 获取 ConfigurationContext 或者 ApplicationContext 对象
     */
    public static Context getContext() {
        return getInstance().getConfigurationContext();
    }

    /**
     * 获取全局的ApplicationContext
     */
    public static Context getApplicationContext() {
        if (getInstance().mApplication != null && getInstance().mApplication.getApplicationContext() != null){
            return getInstance().mApplication.getApplicationContext();
        }
        return getContext();
    }

    public static Context getBaseContext() {
        if (getApplication() != null && getApplication().getBaseContext() != null) {
            return getApplication().getBaseContext();
        }
        if (getInstance().mBase != null) {
            return getInstance().mBase;
        }
        return getContext();
    }

    /**
     * 获取保存的 Application 对象
     */
    public static Application getApplication() {
        return getInstance().mApplication;
    }

    public static void init(@NonNull Application application) {
        getInstance().mApplication = application;
    }

    public static void attachBaseContext(@NonNull Context base) {
        getInstance().mBase = base;
    }

    /**
     * 若有ConfigurationContext 则返回，否则返回 ApplicationContext;
     * 切换了语言（Locale）需要通过这个Context来获取String资源
     */
    public Context getConfigurationContext() {
        if (mConfigurationContext != null) {
            return mConfigurationContext;
        }
        if (mBase != null) {
            return mBase;
        }
        return getApplication();
    }

    /**
     * 设置 ConfigurationContext
     */
    public void setConfigurationContext(Context context) {
        mConfigurationContext = context;
    }

    public static Resources getResources() {
        return getInstance().getConfigurationContext().getResources();
    }

    /**
     * 保存一个全局对象到Map中
     *
     * @param key    对象的key，建议使用Class作为key
     * @param object 对象实例
     */
    public final synchronized void putGlobalObject(Object key, Object object) {
        mGlobalObjects.put(key, object);
    }

    /**
     * 获取对象
     *
     * @param key 对象的key，建议使用Class作为key
     */
    public final synchronized Object getGlobalObject(Object key) {
        return mGlobalObjects.get(key);
    }

    /**
     * 删除在Map中的对象
     *
     * @param key 对象的key
     */
    public final synchronized Object removeGlobalObject(Object key) {
        TGLog.d("removeGlobalObject =-=key===" + key);
        return mGlobalObjects.remove(key);
    }

    /**
     * 通过类加载的机制保证单例的线程安全
     */
    static class LAZY_LOAD {
        @SuppressLint("StaticFieldLeak")
        static final TGApplicationBase sInstance = new TGApplicationBase();
    }
}
